<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('vpage_form.class.php');
require_once('vpage_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_category_dbc.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_sitemap_dbc.class.php');


class VPage_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $templates_ol_map;
	private $htdocs_dir;
	private $hidden_inc_dir;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $htdocs_dir, $hidden_inc_dir) {
		$this->htdocs_dir = tangra_normalize_path($htdocs_dir);
		$this->hidden_inc_dir = tangra_normalize_path($hidden_inc_dir);
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);
		$this->export('htdocs_dir', $this->htdocs_dir);
	}

	protected function create_form() {
		$form = new VPage_Form();

		$dbc = $this->get_dbc();

		$ol_map = TCMS_VPage_Template_DBC::load_templates_ol_map($dbc);
		$types_field = $form->get_field('template');
		array_unshift($ol_map['ids'], 0);
		$ol_map['ol_map'][0] = '---';

		$types_field->set_options($ol_map['ids']);
		$this->templates_ol_map = $ol_map['ol_map'];


//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new VPage_Form_View($this->form);

		$types_field = $ret->get_field_view('template');
		$types_field->set_ol_map($this->templates_ol_map);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if ($this->form->get_field_value('transliterate')) {
			$object = $this->get_object();
			if (!$object->get_id()) {
				$path = str_replace(' ', '-', mb_strtolower(trim($this->form->get_field_value('path'))));
				$this->form->set_field_value('path', $this->transliterate($path));
			}
		}

		$dbc = $this->get_dbc();
		$object = $this->get_object();
		$path = trim($this->form->get_field_value('path'));
		if (substr($path, -1, 1) == '/') {
			$path2 = substr($path, 0, -1);
		} else {
			$path2 = tangra_normalize_path($path);
		}
		if (!(TCMS_VPage_DBC::is_unique_path($dbc, $path, $object->get_id()) &&
				TCMS_VPage_DBC::is_unique_path($dbc, $path2, $object->get_id()))) {
			$this->form->set_field_error('path', 'duplicate_path');
			$ret = false;
		} elseif (strpos($path, '//') !== false || substr($path, 0, 1) == '/') {
			$this->form->set_field_error('path', 'invalid_path');
			$ret = false;
		} elseif (strpos($path, ' ') !== false) {
			$this->form->set_field_error('path', 'invalid_path');
			$ret = false;
		} else {

			if (file_exists($this->htdocs_dir.$path)) {
				$this->form->set_field_error('path', 'normal_exist');
				$this->export('path', $path);
				$ret = false;
			}
		}



		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_VPage_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('path', $object->get_path());
		$form->set_field_value('template', $object->get_template());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_path(trim($form->get_field_value('path')));
		$object->set_template($form->get_field_value('template'));

		if ($object->get_id()) {
			$object->set_last_modified_by($this->get_permanent_external_param('user'));
		} else {
			$object->set_created_by($this->get_permanent_external_param('user'));

			$dbc = $this->get_dbc();
			$template = new TCMS_VPage_Template_DBC();
			$template->load_by_id($dbc, $object->get_template());

			$object->set_cache($template->get_cache());
			$object->set_cache_expiry($template->get_cache_expiry());


			$settings_sitemap = new TCMS_Settings_Sitemap_DBC();
			$settings_sitemap->load_by_id($dbc, 1);

			$object->set_sitemap_weight($settings_sitemap->get_weight());
			$object->set_sitemap_change_freq($settings_sitemap->get_change_freq());
		}
	}


	protected function on_trans_ending(DB_Connection $dbc) {
		$object = $this->get_object();

		$category_id = $this->get_permanent_external_param('category_id', true);
		if ($category_id) {
			$category = new TCMS_VPage_Category_DBC();
			$category->set_vpage($object->get_id());
			$category->set_category($category_id);
			$category->set_ordering(TCMS_VPage_Category_DBC::get_next_ordering($dbc, $category_id));
			$category->save($dbc);
		}


		$template = new TCMS_VPage_Template_DBC();
		$template->load_by_id($dbc, $object->get_template());

		$class_name = $template->get_wp_class_name();
		if (!class_exists($class_name)) {
			require_once($this->hidden_inc_dir.$template->get_wp_path());
		}

		call_user_func_array(array($class_name, 'new_created'), array($dbc, $object));
	}


	private function transliterate($path) {
		$tpath = '';

		$t_arr = array( 'а' => 'a',
						'б' => 'b',
						'в' => 'v',
						'г' => 'g',
						'д' => 'd',
						'е' => 'e',
						'ж' => 'j',
						'з' => 'z',
						'и' => 'i',
						'й' => 'y',
						'к' => 'k',
						'л' => 'l',
						'м' => 'm',
						'н' => 'n',
						'о' => 'o',
						'п' => 'p',
						'р' => 'r',
						'с' => 's',
						'т' => 't',
						'у' => 'u',
						'ф' => 'f',
						'х' => 'h',
						'ц' => 'c',
						'ч' => 'ch',
						'ш' => 'sh',
						'щ' => 'sht',
						'ъ' => 'a',
						'ь' => 'x',
						'ю' => 'yu',
						'я' => 'ya'
						);

		for($i=0; $i <= tangra_strlen($path); $i++) {
			$char = mb_substr($path, $i, 1);
			if (array_key_exists($char, $t_arr)) {
				$tpath .= $t_arr[$char];
			} else {
				$tpath .= $char;
			}
		}

		return $tpath;
	}
}